package com.glory.huaweipda.viewmodel

import android.app.Activity
import android.os.Handler
import androidx.databinding.ObservableArrayList
import com.glory.huaweipda.BR
import com.glory.huaweipda.R
import com.glory.huaweipda.bean.EqpLogBean
import com.glory.huaweipda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.google.gson.JsonObject
import me.tatarka.bindingcollectionadapter2.ItemBinding

class ScanStartModel : BaseViewModel() {

    var mfgItems = ObservableArrayList<EqpLogBean.Logmessagetextlist>()
    var itemBinding =
        ItemBinding.of<EqpLogBean.Logmessagetextlist>(BR.eqpLog, R.layout.item_scan_layout)
    private val repository by lazy { TaskRepository() }

    fun initAssets(context: Activity) {
        initData()

    }

    fun initData() {

    }

    fun defaultGetEQPLog(eqpID: String) {
        val map = JsonObject()
        map.addProperty("Eqpid", eqpID)

        launchOnlyResult({

            val defaultGetEQPLog =
                repository.defaultGetEQPLog(map)
            if (defaultGetEQPLog != null) {
                mfgItems.clear()
//                var itemList = List<EqpLogBean.Logmessagetextlist>
                var logmessagetextlists = ArrayList<EqpLogBean.Logmessagetextlist>()
                logmessagetextlists.clear()
                for (index in defaultGetEQPLog.logmessagetextlist.size-1 downTo 0){
                    logmessagetextlists.add(defaultGetEQPLog.logmessagetextlist[index])
                }
                mfgItems.addAll(logmessagetextlists.distinctBy { it.messageid })
                Handler().postDelayed({
                    defUI.callObserve.value = CallObserveBean(1,defaultGetEQPLog)
                },3*1000)
            }
        }, false)
    }

    fun defaultTrackIn(eqpID: String, user: String, lotID: String) {

        val map = JsonObject()
        map.addProperty("Lotid", lotID)
        map.addProperty("Eqpid", eqpID)
        map.addProperty("Opid", user)

        launchOnlyResult({
            repository.defaultTrackIn(map)
            defUI.callObserve.value = CallObserveBean(200)
        })
    }

    fun defaultLotInfo(eqpID: String, user: String, lotID: String) {
        val map = JsonObject()
        map.addProperty("Lotid", lotID)
        map.addProperty("Eqpid", eqpID)
        map.addProperty("Opid", user)
//        map.addProperty("TrackStatus", "TRACKIN")
        map.addProperty("TrackStatus", "")
        map.addProperty("IsUploadSpc", "")

        launchOnlyResult({
            val defaultLotInfo =
                repository.defaultLotInfo(map)

            defUI.callObserve.value = CallObserveBean(0, defaultLotInfo.lots.get(0))
        })
    }

}